package com.hui.stock.controller;


import com.hui.stock.pojo.domain.RolesInfoDomain;
import com.hui.stock.pojo.domain.UserInfoDomain;
import com.hui.stock.pojo.entity.SysUser;
import com.hui.stock.service.UserService;
import com.hui.stock.vo.req.AddUserReqVo;
import com.hui.stock.vo.req.LoginReqVo;
import com.hui.stock.vo.req.UpdateUserRoleReqVo;
import com.hui.stock.vo.req.UserPageInfoReqVo;
import com.hui.stock.vo.resp.LoginRespVo;
import com.hui.stock.vo.resp.PageResult;
import com.hui.stock.vo.resp.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Api(tags = "用户相关接口处理")
@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 根据用户名查询用户信息
     * @param name
     * @return
     */
    @ApiOperation(value = "根据用户名查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "用户名",dataType = "String",required = true,type = "path")
    })
    @GetMapping("/user/{username}")
    public SysUser getUserByName(@PathVariable("username") String name){
        return userService.getUserByUserName(name);
    }

    // @ApiOperation(value = "用户登录功能")
    // @PostMapping("/login")
    // public R<LoginRespVo> login(@RequestBody LoginReqVo vo, HttpSession session){
    //     R<LoginRespVo> loginRespVoR = userService.login(vo);
    //     if (loginRespVoR.getCode()==1){
    //         Long id = loginRespVoR.getData().getId();
    //         session.setAttribute("userId",id);
    //         System.out.println(session.getAttribute("userId"));
    //     }
    //     return loginRespVoR;
    // }

    @ApiOperation(value = "验证码生成")
    @GetMapping("/captcha")
    public R<Map> getCaptchaCode(){
        return userService.getCaptchaCode();
    }

    @ApiOperation(value = "多条件综合查询用户分页信息")
    @PostMapping("/users")
    public R<PageResult> queryUserPageInfo(@RequestBody UserPageInfoReqVo userPageInfoReqVo){
        return userService.queryUserPageInfo(userPageInfoReqVo);
    }

    @ApiOperation("添加用户")
    @PostMapping("/user")
    public R addUser(@RequestBody AddUserReqVo addUserReqVo,HttpSession session){
        System.out.println("sessionID:"+session.getAttribute("userId"));
        return userService.addUser(addUserReqVo,session);
    }

    /**
     * 获取用户具有的角色信息，以及所有角色信息
     * @param userId
     * @return
     */
    @ApiOperation("获取用户具有的角色信息，以及所有角色信息")
    @GetMapping("/user/roles/{userId}")
    public R<RolesInfoDomain> getRolesByUserId(@PathVariable Long userId){
        return userService.getRolesByUserId(userId);
    }

    @ApiOperation("更新用户角色信息")
    @PutMapping("/user/roles")
    public R updateUserRoles(@RequestBody UpdateUserRoleReqVo updateUserRoleReqVo){
        return userService.updateUserRoles(updateUserRoleReqVo);
    }

    @ApiOperation("删除用户信息，delete请求可通过请求体携带数据")
    @DeleteMapping("/user")
    public R deleteUserByUserID(@RequestBody List<Long> userIds){
        return userService.deleteUserByUserID(userIds);
    }

    @ApiOperation("根据用户id查询用户信息")
    @GetMapping("/user/info/{userId}")
    public R<UserInfoDomain> getUserInfoByUserId(@PathVariable Long userId){
        return userService.getUserInfoByUserId(userId);
    }

    @ApiOperation("根据id更新用户基本信息")
    @PutMapping("/user")
    public R updateUserInfo(@RequestBody SysUser sysUser,HttpSession session){
        return userService.updateUserInfo(sysUser,session);
    }

}
